/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.vntnumberpickerpreference.sample.slice;

import com.andexert.library.RippleView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupAnimation;
import com.lxj.xpopup.interfaces.SimpleCallback;
import com.vanniktech.vntnumberpickerpreference.Constants;
import com.vanniktech.vntnumberpickerpreference.CustomPickerPopup;
import com.vanniktech.vntnumberpickerpreference.ParameterEntity;
import com.vanniktech.vntnumberpickerpreference.PreferenceUtils;
import com.vanniktech.vntnumberpickerpreference.sample.MainAbility;
import com.vanniktech.vntnumberpickerpreference.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;

/**
 * SettingsAbilitySlice
 *
 * @since 2021-06-15
 */
public class SettingsAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private static final int FONTMINVALUE = 8;
    private static final int FONTMAXVALUE = 100;
    private static final int BODYMINVALUE = 40;
    private static final int BODYMAXVALUE = 250;
    private static final int CALLBACKMAXVALUE = 10;
    private Text fontTitle;
    private Text bodyTitle;
    private Text callbackTitle;
    private Text customTitle;
    private Text fontContent;
    private Text bodyContent;
    private Text callbackContent;
    private Text customContent;
    private final ParameterEntity parameterEntity = new ParameterEntity();

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_settings);
        initComponent();
        initData();
    }

    private void initComponent() {
        Image imageFinish = (Image) findComponentById(ResourceTable.Id_image_finish);
        imageFinish.setClickedListener(this);
        RippleView rvFont = (RippleView) findComponentById(ResourceTable.Id_rv_font);
        rvFont.setClickedListener(this);
        RippleView rvBody = (RippleView) findComponentById(ResourceTable.Id_rv_body);
        rvBody.setClickedListener(this);
        RippleView rvCallback = (RippleView) findComponentById(ResourceTable.Id_rv_callback);
        rvCallback.setClickedListener(this);
        RippleView rvCustom = (RippleView) findComponentById(ResourceTable.Id_rv_custom);
        rvCustom.setClickedListener(this);
        fontTitle = (Text) findComponentById(ResourceTable.Id_font_title);
        bodyTitle = (Text) findComponentById(ResourceTable.Id_body_title);
        callbackTitle = (Text) findComponentById(ResourceTable.Id_callback_title);
        customTitle = (Text) findComponentById(ResourceTable.Id_custom_title);
        fontContent = (Text) findComponentById(ResourceTable.Id_font_content);
        bodyContent = (Text) findComponentById(ResourceTable.Id_body_content);
        callbackContent = (Text) findComponentById(ResourceTable.Id_callback_content);
        customContent = (Text) findComponentById(ResourceTable.Id_custom_content);
    }

    private void initData() {
        fontContent.setText(PreferenceUtils.getString(getApplicationContext(), Constants.FONT_SIZE, "25"));
        bodyContent.setText(PreferenceUtils.getString(getApplicationContext(), Constants.BODY_SIZE, "180"));
        callbackContent.setText(PreferenceUtils.getString(getApplicationContext(), Constants.CALLBACK, "1"));
        customContent.setText(PreferenceUtils.getString(getApplicationContext(), Constants.CUSTOM_SUMMARY, "10"));
        parameterEntity.setContext(getContext());
        parameterEntity.setAbilitySlice(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_finish:
                finish();
                break;
            case ResourceTable.Id_rv_font:
                font();
                break;
            case ResourceTable.Id_rv_body:
                body();
                break;
            case ResourceTable.Id_rv_callback:
                callback();
                break;
            case ResourceTable.Id_rv_custom:
                custom();
                break;
            default:
        }
    }

    private void finish() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
            .withDeviceId("")
            .withBundleName(getAbilityPackageContext().getBundleName())
            .withAbilityName(MainAbility.class.getName())
            .build();
        intent.setOperation(operation);
        startAbility(intent);
        terminateAbility();
    }

    private void font() {
        parameterEntity.setKey(Constants.FONT_SIZE);
        parameterEntity.setMinValue(FONTMINVALUE);
        parameterEntity.setMaxValue(FONTMAXVALUE);
        parameterEntity.setWheelModeEnabled(true);
        parameterEntity.setValue(fontContent.getText());
        parameterEntity.setTitle(fontTitle.getText());
        new XPopup.Builder(getContext())
            .popupAnimation(PopupAnimation.NoAnimation)
            .setPopupCallback(new SimpleCallback() {
                @Override
                public void onDismiss(BasePopupView basePopupView) {
                    // 完全隐藏的时候执行
                    fontContent.setText(PreferenceUtils.getString(getApplicationContext(),
                        Constants.FONT_SIZE, "25"));
                }
            })
            .asCustom(new CustomPickerPopup(parameterEntity))
            .show();
    }

    private void body() {
        parameterEntity.setKey(Constants.BODY_SIZE);
        parameterEntity.setMinValue(BODYMINVALUE);
        parameterEntity.setMaxValue(BODYMAXVALUE);
        parameterEntity.setWheelModeEnabled(false);
        parameterEntity.setValue(bodyContent.getText());
        parameterEntity.setTitle(bodyTitle.getText());
        new XPopup.Builder(getContext())
            .popupAnimation(PopupAnimation.NoAnimation)
            .setPopupCallback(new SimpleCallback() {
                @Override
                public void onDismiss(BasePopupView basePopupView) {
                    // 完全隐藏的时候执行
                    bodyContent.setText(PreferenceUtils.getString(getApplicationContext(),
                        Constants.BODY_SIZE, "180"));
                }
            })
            .asCustom(new CustomPickerPopup(parameterEntity))
            .show();
    }

    private void callback() {
        parameterEntity.setKey(Constants.CALLBACK);
        parameterEntity.setMinValue(0);
        parameterEntity.setMaxValue(CALLBACKMAXVALUE);
        parameterEntity.setWheelModeEnabled(false);
        parameterEntity.setValue(callbackContent.getText());
        parameterEntity.setTitle(callbackTitle.getText());
        new XPopup.Builder(getContext())
            .popupAnimation(PopupAnimation.NoAnimation)
            .setPopupCallback(new SimpleCallback() {
                @Override
                public void onDismiss(BasePopupView basePopupView) {
                    // 完全隐藏的时候执行
                    callbackContent.setText(PreferenceUtils.getString(getApplicationContext(),
                        Constants.CALLBACK, "1"));
                }
            })
            .asCustom(new CustomPickerPopup(parameterEntity))
            .show();
    }

    private void custom() {
        parameterEntity.setKey(Constants.CUSTOM_SUMMARY);
        parameterEntity.setMinValue(0);
        parameterEntity.setMaxValue(FONTMAXVALUE);
        parameterEntity.setWheelModeEnabled(false);
        parameterEntity.setValue(customContent.getText());
        parameterEntity.setTitle(customTitle.getText());
        new XPopup.Builder(getContext())
            .popupAnimation(PopupAnimation.NoAnimation)
            .setPopupCallback(new SimpleCallback() {
                @Override
                public void onDismiss(BasePopupView basePopupView) {
                    // 完全隐藏的时候执行
                    customContent.setText("My custom summary text. Value is "
                        + PreferenceUtils.getString(getApplicationContext(), Constants.CUSTOM_SUMMARY, "10"));
                }
            })
            .asCustom(new CustomPickerPopup(parameterEntity))
            .show();
    }
}
